package cn.ococci.dm.Main.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.ococci.dm.Main.Model.HeaderModel;
import cn.ococci.dm.Main.Model.UnitModel;
import cn.ococci_dm.R;


/**
 * Created by cyan on 15/12/29.
 */
public class SelectUnitExpandAdapter extends BaseExpandableListAdapter {

    private Context context;

    private List<HeaderModel> headerModelList;

    public SelectUnitExpandAdapter(Context context,List<HeaderModel> headerModelList){
        this.context = context;
        this.headerModelList = headerModelList;
    }


    @Override
    public int getGroupCount() {
        return headerModelList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return headerModelList.get(groupPosition).getModelList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return headerModelList.get(groupPosition).getModelList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }


    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        GroupHolder gh;
        if (convertView == null) {
            gh = new GroupHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.fragment_select_unit_group_item, parent, false);

            gh.groupNameTv = (TextView) convertView.findViewById(R.id.groupNameTv);
            convertView.setTag(gh);
        } else
            gh = (GroupHolder) convertView.getTag();

        gh.groupNameTv.setText(headerModelList.get(groupPosition).getTitle());

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        ChildHolder ch;
        if (convertView == null) {
            ch = new ChildHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.fragment_select_unit_lv_item, parent, false);
            ch.selectedImage = (ImageView) convertView.findViewById(R.id.selectedImage);
            ch.unitNameTv = (TextView) convertView.findViewById(R.id.unitNameTv);

            convertView.setTag(ch);
        } else
            ch = (ChildHolder) convertView.getTag();


        UnitModel unitModel = headerModelList.get(groupPosition).getModelList().get(childPosition);

        ch.unitNameTv.setText(unitModel.getUnitName());
        ch.selectedImage.setImageResource(unitModel.isSelected()?R.mipmap.selected_img:R.mipmap.disselect_image);


        return convertView;
    }

    static class GroupHolder {

        TextView groupNameTv; //名称

    }

    static class ChildHolder {
        TextView unitNameTv;  //* 名称
        ImageView selectedImage; //选中状态
    }



}
